package com.zdp.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.stereotype.Component;

/**
 * 类描述: 实现Zuul的Post过滤器
 *
 * @author ZhhengDP
 * @since 2021/8/31
 */
@Component
public class ZuulPostFilter  extends ZuulFilter {
    private static final int FILTER_ORDER = 1;
    private static final boolean SHOULD_FILTER=true;

    @Autowired
    private Tracer tracer;

    @Override
    public String filterType() {
        return "post";
    }

    @Override
    public int filterOrder() {
        return FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        return SHOULD_FILTER;
    }

    @Override
    public Object run() {
        // 可以先判断当前环境是否为生产环境，如果不是生成环境，才将traceId设置到响应头中
        //获取到TraceId，并将其设置到响应中
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.getResponse().addHeader("scd-trace-id",
                this.tracer.getCurrentSpan().traceIdString());
        return null;
    }
}
